package com.hmyd.hmyd_android.fragment;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.utils.Utils;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;

/**
 * 引导页1
 * 
 * @author wangc
 * 
 */
public class GuideOneFragment extends Fragment {
	private View rootView;
	private ImageView mIvTitle;
	private ImageView mIvContent;
	private ImageView mIvLogo;

	float mIvTitleX = 0;
	float mIvContentX = 0;
	float mIvLogoY = 0;
	private boolean isShowed = false;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.view_guide_one, container, false);
		return rootView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initview();
		runAnimator();
	}

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);

		if (isVisibleToUser && !isShowed) {
			
		}
	}

	@SuppressLint("NewApi")
	private void initview() {
		mIvContent = (ImageView) rootView.findViewById(R.id.tv_content);
		mIvTitle = (ImageView) rootView.findViewById(R.id.tv_title);
		mIvLogo = (ImageView) rootView.findViewById(R.id.guide_logo);

		if (Build.VERSION.SDK_INT >= 11) {
			mIvLogo.setAlpha(0f);
			mIvTitle.setAlpha(0f);
			mIvContent.setAlpha(0f);
		} else {
			Drawable d = this.getResources().getDrawable(R.drawable.hui_logo);
			d.setAlpha(0);
			mIvLogo.setImageDrawable(d);

			Drawable a = this.getResources().getDrawable(R.drawable.hui_biao);
			d.setAlpha(0);
			mIvTitle.setImageDrawable(a);

			Drawable c = this.getResources().getDrawable(R.drawable.hui_tx);
			d.setAlpha(0);
			mIvContent.setImageDrawable(c);
		}
	}

	private void runAnimator() {
		isShowed = true;
		mIvTitle.post(new Runnable() {
			@Override
			public void run() {
				mIvTitleX = mIvTitle.getMeasuredWidth();

				AnimatorSet set = new AnimatorSet();
				mIvTitle.setPadding(-mIvTitle.getWidth(), 0, 0, 0);

				set.playTogether(ObjectAnimator.ofFloat(mIvTitle, "translationX",
						-mIvTitle.getWidth(), Utils.getScreenWith(getActivity()) / 2 - mIvTitleX
								/ 2), ObjectAnimator.ofFloat(mIvTitle, "alpha", 0f, 1f));
				set.setDuration(500).start();
				mIvTitle.setPadding(0, 0, 0, 0);
			}
		});

		mIvContent.post(new Runnable() {
			@Override
			public void run() {
				mIvContentX = mIvContent.getMeasuredWidth();

				AnimatorSet set = new AnimatorSet();
				mIvContent.setPadding(-mIvContent.getWidth(), 0, 0, 0);

				set.playTogether(
						ObjectAnimator.ofFloat(mIvContent, "translationX",
								Utils.getScreenWith(getActivity()) + mIvContent.getWidth(),
								-Utils.getScreenWith(getActivity()) / 2 + mIvContentX / 2),
						ObjectAnimator.ofFloat(mIvContent, "alpha", 0f, 1f));
				set.setDuration(500).start();
				mIvContent.setPadding(0, 0, 0, 0);
			}
		});

		mIvLogo.post(new Runnable() {
			@Override
			public void run() {
				mIvLogoY = mIvLogo.getMeasuredWidth();

				AnimatorSet set = new AnimatorSet();
				mIvLogo.setPadding(0, -mIvLogo.getHeight(), 0, 0);
				set.playTogether(
						ObjectAnimator.ofFloat(mIvLogo, "translationY", -mIvLogo.getHeight(),
								Utils.getScreenHeight(getActivity()) / 3 - mIvLogoY / 2),
						ObjectAnimator.ofFloat(mIvLogo, "alpha", 0f, 1f));
				set.setDuration(500).start();
				mIvLogo.setPadding(0, 0, 0, 0);
			}
		});
	}
}
